package com.xwx.common.model;
import java.util.ArrayList;
import java.util.List;
import com.jfinal.kit.StrKit;
import com.xwx.common.utils.Condition;

/**
 * 角色model
 *
 * @author Drean QQ:68697015
 *
 * 日期：下午10:12:50
 */
@SuppressWarnings("serial")
public class RoleModel extends ModelExt<RoleModel>{
	public static final RoleModel dao = new RoleModel();
	/**
	 * 角色授权
	 * @param roleId 角色ID
	 * @param funId 模块ID
	 */
	public static void saveRoleAccess(String roleId,String funId){
		saveRoleAccess(roleId, new String[]{funId});
	}
	/**
	 * 角色授权
	 * @param roleId 角色ID
	 * @param funIds 模块IDS
	 */
	public static void saveRoleAccess(String roleId,String[] funIds){
		RoleAccessModel.clearUserAccessData(roleId);
		for (String funId : funIds) {
			FunModel fm = FunModel.dao.findById(funId);
			RoleAccessModel ram = new RoleAccessModel();
			ram.set("roleId", roleId).set("funId", funId).set("level",fm.getInt("level")).set("funPid",fm.getStr("pId")).save();
		}
	}
	/**
	 * 查询角色权限
	 * @param roleId 角色
	 * @return
	 */
	public static List<FunModel> queryRoleAccess(String roleId){
		return queryRoleAccess(new String[]{roleId});
	}
	/**
	 * 查询角色权限 
	 * @param roleIds 角色数组
	 * @return
	 */
	public static List<FunModel> queryRoleAccess(String...roleIds){
		List<FunModel> funList = new ArrayList<FunModel>();
		String roleStr="";
		for (String str : roleIds) {
			roleStr+=str+",";
		}
		if(StrKit.notBlank(roleStr)){
			List<RoleAccessModel> accessList=RoleAccessModel.dao.queryListByCondition(new Condition().in("roleId", roleStr).toString());
			for (RoleAccessModel ram : accessList) {
				funList.add(FunModel.dao.findById(ram.getStr("funId")));
			}
		}
		return funList;
	}
}
